package com.c.ctgproject.ui.doorkeeper.part

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.R
import com.c.ctgproject.adapter.doorkeeper.AllSellerAdapter
import com.c.ctgproject.adapter.doorkeeper.DoorkeeperPartListAdapter
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.entity.AllSellerChildEntity
import com.c.ctgproject.entity.AllSellerEntity
import com.c.ctgproject.entity.doorkeeper.DoorkeeperPartOutEntity
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_doorkeeper_acticity.mCarNumberET
import kotlinx.android.synthetic.main.activity_doorkeeper_acticity.mCleanIV
import kotlinx.android.synthetic.main.activity_doorkeeper_acticity.mDoorkeeperRV
import kotlinx.android.synthetic.main.activity_doorkeeper_acticity.mRefresh
import kotlinx.android.synthetic.main.activity_doorkeeper_acticity.mSearchTV
import kotlinx.android.synthetic.main.activity_doorkeeper_part.*

/**
 *作者:LiBW
 *创建日期: 2020/9/12
 *描述:门卫 出厂 配件出场 页面
 */
class DoorkeeperPartActivity : BaseActivity(), DoorkeeperPartListAdapter.DoorkeeperPartCallback {
    //待入场车辆列表信息
    private var mList: ArrayList<DoorkeeperPartOutEntity.DataBean.ListBean> = ArrayList()
    private var searchList: ArrayList<DoorkeeperPartOutEntity.DataBean.ListBean> = ArrayList()
    private var copyList: ArrayList<DoorkeeperPartOutEntity.DataBean.ListBean> = ArrayList()
    private var page = 1
    private var size = 10
    private val mViewModel: DoorkeeperPartViewModel by lazy { ViewModelProvider(this)[DoorkeeperPartViewModel::class.java] }
    private val mAdapter: DoorkeeperPartListAdapter by lazy { DoorkeeperPartListAdapter(this, mList, this) }
    private val sellerList = ArrayList<AllSellerChildEntity>()
    private val sellAdapter: AllSellerAdapter by lazy { AllSellerAdapter(this, sellerList) }
    //搜索字段
    private var searchText = ""
    private var searchId = 0
    override fun getLayout() = R.layout.activity_doorkeeper_part
    override fun event() {
        changeTransparentStatusBar()
        mDoorkeeperRV.adapter = mAdapter
        mDoorkeeperRV.layoutManager = LinearLayoutManager(this)
        //添加销售第一条信息
        val firstEntity = AllSellerChildEntity()
        firstEntity.salesPersonName = "选择销售"
        firstEntity.userId = 0
        sellerList.add(firstEntity)
        mSellerSpinner.adapter = sellAdapter

        mViewModel.listResult.observe(this, Observer {
            loading.dismiss()
            mRefresh.over()
            MyLog.d("门卫配件出场列表:$it")
            it.handlerCall({
                val entity = getGson().fromJson(it, DoorkeeperPartOutEntity::class.java).data
                entity?.list?.let { list ->
                    mList.addAll(list)
                    copyList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                }
            }, { error ->
                showToast(error)
            })
        })

        //获取多角色
        mViewModel.getAllSeller()
        mViewModel.allSeller.observe(this, Observer {
            loading.dismiss()
            MyLog.d("获取所有销售回调:$it")
            it.handlerCall({
                val entity = getGson().fromJson(it, AllSellerEntity::class.java).data
                entity?.let {
                    sellerList.addAll(entity)
                    sellAdapter.notifyDataSetChanged()
                }
            }, { msg ->
                MyLog.d(msg)
                showToast(msg)
            })
        })
        //确认出场回调
        mViewModel.confirmResult.observe(this, Observer {
            loading.dismiss()
            MyLog.d("确认出场回调:$it")
            it.handlerCall({
                showToast("确认出场成功")
                initData()
            }, { msg ->
                MyLog.d(msg)
                showToast(msg)
            })

        })

        mViewModel.error.observe(this, Observer {
            mRefresh.over()
            MyLog.d("失败回调:$it")
            loading.dismiss()
            showToast(it)
        })
    }

    override fun click() {
        //角色选择
        mSellerSpinner.handlerSelect {
            MyLog.d("选择了${sellerList[it].salesPersonName}")
            if (sellerList[it].userId != 0) {
                searchId=sellerList[it].userId
                loading.show()
                mList.clear()
                copyList.clear()
                searchList.clear()
                page=1
                mViewModel.getPartOutList(page, size, searchText, searchId)
            }
        }

        //搜索框清除按钮
        mCleanIV.setOnClickListener {
            mCarNumberET.text = setEditText("")
        }
        //搜索输入框监听
        mCarNumberET.handlerText { searchText ->
            MyLog.d("搜索内容$searchText")
            if (searchText.isEmpty()) {
                mList.clear()
                mList.addAll(copyList)
                mAdapter.notifyDataSetChanged()
                return@handlerText
            }
            //清除临时列表
            searchList.clear()
            //搜索数据列表如果搜索内容存在则加入到临时列表
            copyList.forEach {
                if (it.partsSalesNo.toString().contains(searchText)) {
                    MyLog.d("找到销售单号${it.partsSalesNo}")
                    searchList.add(it)
                }
            }
            //清除当前列表并将临时列表内容放入数据列表
            mList.clear()
            mList.addAll(searchList)
            //通知列表刷新
            mAdapter.notifyDataSetChanged()
        }
        //搜索按钮
        mSearchTV.setOnClickListener {
            page = 1
            searchText = mCarNumberET.text.toString()
            mList.clear()
            loading.show()
            mViewModel.getPartOutList(page, size, searchText, searchId)
        }

        //下拉刷新监听
        mRefresh.setOnRefreshListener {
            initData()

        }
        //上拉加载
        mRefresh.setOnLoadMoreListener {
            page++
            loading.show()
            mViewModel.getPartOutList(page, size, searchText, searchId)
        }
    }

    private fun initData() {
        mCarNumberET.text = setEditText("")
        mList.clear()
        searchList.clear()
        copyList.clear()
        page = 1
        loading.show()
        //获取列表
        mViewModel.getPartOutList(page, size)
    }

    override fun onResume() {
        super.onResume()
        initData()
    }

    //确认出厂
    override fun carConfirm(partsSalesOrderId: Int) {
        if (partsSalesOrderId != 0) {
            mViewModel.confirmPartOut(partsSalesOrderId)
        } else {
            showToast("ID有误")
        }
    }
}